package com.shuzixiangcun.controller.admin;

import com.github.pagehelper.PageInfo;
import com.shuzixiangcun.entity.JobPostings;

import com.shuzixiangcun.entity.Users;
import com.shuzixiangcun.service.admin.JobPostingsServiceImpl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 招工表(JobPostings)表控制层
 *
 * @author makejava
 * @since 2024-06-24 08:26:15
 */
@RestController
@RequestMapping("admin/jobPostings")
@CrossOrigin
public class JobPostingsController {
    /**
     * 服务对象
     */
    @Resource
    private JobPostingsServiceImpl jobPostingsService;



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<JobPostings> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.jobPostingsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param jobPostings 实体
     * @return 新增结果
     */
    @PostMapping("addJobPstings")
    public ResponseEntity<JobPostings> add(JobPostings jobPostings) {
        return ResponseEntity.ok(this.jobPostingsService.insert(jobPostings));
    }

    /**
     * 编辑数据
     *
     * @param jobPostings 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<JobPostings> edit(JobPostings jobPostings) {
        return ResponseEntity.ok(this.jobPostingsService.update(jobPostings));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("jobId")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.jobPostingsService.deleteById(id));
    }

    @GetMapping("/getListPostings")
    public ResponseEntity getListPostings(HttpServletRequest request, @RequestParam(required = false)String jobTitle) {
        return ResponseEntity.ok(this.jobPostingsService.queryListJobPostings(jobTitle));
    }

    @GetMapping("/PageInfo")
    public ResponseEntity<PageInfo<JobPostings>> getJobPostingsList(@RequestParam Integer pageNo, @RequestParam Integer pageSize){

        return ResponseEntity.ok(this.jobPostingsService.getPageInfo(pageNo,pageSize));
    }

    @PostMapping("updateByState")
    public ResponseEntity updateByState(JobPostings jobPostings){
        String state1=jobPostings.getState().equals("Y")?"N":"Y";

        jobPostings.setState(state1);
        if (this.jobPostingsService.updateByState(jobPostings)>0){
            return ResponseEntity.status(200).header("Custom-Header", "foo")
                    .body("修改成功");
        }
        return ResponseEntity.status(500).header("Custom-Header", "foo")
                .body("修改失败");
    }

}

